from Tkinter import *
from tkMessageBox import *
from svn_interface import *

class LoginUI(object):
    """
    Obtains user credentials.
    """
    # TODO: generalize beyond Google Code!
    def __init__(self, master, callback):
        print "Logging In..."
        self.callback = callback
        self.master = Toplevel(master)
        self.master.title('Type your Google Code email and password')
        Label(self.master, text='Gmail Address').grid(row=0, column=0)
        self.username_entry = Entry(self.master)
        self.username_entry.grid(row=0, column=1, sticky=E)
        Label(self.master, text='Google Code Password').grid(row=1, column=0)
        self.password_entry = Entry(self.master, show="*")
        self.password_entry.grid(row=1, column=1, sticky=E)
        self.username_entry.bind('<Return>', self.password_entry.focus_set)
        self.password_entry.bind('<Return>', self.login)
        gc_button = Button(self.master, text='Get Google Code Password', command=find_google_code_password)
        gc_button.grid(row=2, column=0, columnspan=2)
        login_button = Button(self.master, text='Log In', command=self.login)
        login_button.grid(row=3, column=1)
        cancel_button = Button(self.master, text='Cancel', command=self.master.destroy)
        cancel_button.grid(row=3, column=0)
        self.master.resizable(width=FALSE, height=FALSE)
        self.username_entry.focus_set()
        self.credentials = None

    def login(self, event):
        self.credentials = Credentials(self.username_entry.get(), \
            self.password_entry.get())
        self.master.destroy()
        self.callback(self.credentials)

